package camera.view;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import camera.CameraHolder;
import camera.mediarecorder.CameraRecorder;

/**
 * @author:ike
 * @function: 相机预览
 * @creat time:14:55 2017/12/29
 **/

public class RenderSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer {
    private MySurfaceCallBackListener listener;
    private CameraHolder mCameraHolder;
    private CameraRender cameraRender;


    public RenderSurfaceView(Context context) {
        this(context, null);
    }

    public RenderSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 设置OpenGl版本号,Render，以及渲染模式
     */
    private void init() {
       
        setEGLContextClientVersion(2);
        cameraRender = new CameraRender(this);
        setRenderer(cameraRender);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        //设置SurfaceHolder
        SurfaceHolder holder = getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        listener = new MySurfaceCallBackListener();
        holder.addCallback(listener);
        //实例化相机设置对象
        mCameraHolder = CameraHolder.instance();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {

    }

    /**
     * surfaceHolder的回调监听
     */
    public class MySurfaceCallBackListener implements SurfaceHolder.Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //停止相机预览，并且释放相机资源
            mCameraHolder.stopPreview();
            mCameraHolder.releaseCamera();
        }
    }
    public void  startRecord(CameraRecorder cameraRecorder){
       cameraRender.setCameraRecorder(cameraRecorder);
    }
}
